﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using Newtonsoft.Json.Linq;

using CanDoo;
using CanDoo.Core;
using CanDoo.Data;
using CanDoo.Web;
using CanDoo.Secutiry;
using CanDoo.FineUI;
using CanDoo.Extensions;
using CanDoo.Core.Context;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Admin.Core.Models;
using CanDoo.Admin.Core.Contracts;
using FineUIPro;

namespace CanDoo.Oil.WebApp.cusCustomer
{
    public partial class Edit : EditPagebase<ICustomerContract, Customer>
    {
        private const string CONFIGPASSWORD = "%$@^HU^%W#G(%^G(";
        public ICustomerLevelContract CustomerLevelContract { get; set; }
        public IProductContract ProductContract { get; set; }
        public IUserContract UserContract { get; set; }
        public ICustomerContract CustomerContract { get; set; }
        public IChinaAreaContract ChinaAreaContract { get; set; }
        public IOperator Operator { get; set; }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FineUIBindList.BindSex(ed_Sex, true);
                FineUIListHelper.BindDDL(CustomerLevelContract.EntityName, this.CustomerLevelContract.GetSelectList(), 0L, ed_CustomerLevel,true);
                FineUIListHelper.BindDDL(ProductContract.EntityName,ProductContract.GetSelectList(p=>p.ProductClass==1 && p.Enable==false), 0L, ed_LimitProduct);

                FineUIListHelper.BindDDL("省份", ChinaAreaContract.GetSelectList(),0L, ed_Province);
                var provinceCode = ed_Province.GetValue();
                FineUIListHelper.BindDDL("城市", ChinaAreaContract.GetSelectList(p=>p.ParentCode == provinceCode), 0L, ed_City);
                var cityCode = ed_City.GetValue();
                FineUIListHelper.BindDDL("县区", ChinaAreaContract.GetSelectList(p => p.ParentCode == cityCode), 0L, ed_Area);
                var areaCode = ed_Area.GetValue();
                FineUIListHelper.BindDDL("街道", ChinaAreaContract.GetSelectList(p => p.ParentCode == areaCode), 0L, ed_Street);

                if (PageMode != pagemode.Add)
                    LoadData();
                else
                {
                    int CardType = Web.Request.QueryString("CardType").ToInt();
                    ed_CardType.SetValue(CardType);
                    ControlSet(CardType);
                    ed_Password.SetValue("888888");
                    ed_RePassword.SetValue("888888");
                }
            }
        }

        public void ControlSet(int CardType)
        {
            if (CardType != 0)
                ed_CardType.Readonly = true;
            else
                ed_CardType.Readonly = false;

            if (CardType == 2)
                ed_LimitProduct.Hidden = false;
            else
                ed_LimitProduct.Hidden = true;

            if (CardType == 3)
                ed_Introduction.Hidden = false;
            else
                ed_Introduction.Hidden = true;
        }

        protected override void LoadData(Customer entity)
        {
            ed_Name.SetValue(entity.Name);
            ed_CardNo.SetValue(entity.CardNo);
            ed_Password.SetValue(CONFIGPASSWORD);
            ed_RePassword.SetValue(CONFIGPASSWORD);
            ed_Sex.SetValue(entity.Sex);
            ed_MobilePhone.SetValue(entity.MobilePhone);
            ed_IDCard.SetValue(entity.IDCard);
            //ed_ImageUrl.SetValue(entity.ImageUrl);
            ed_CardBank.SetValue(entity.CardBank);
            ed_CardName.SetValue(entity.CardName);

            var m = ed_Province.Items.FindByText(entity.Province);
            if (m != null)
                ed_Province.SelectedValue = m.Value;


            var provinceCode = ed_Province.GetValue();
            FineUIListHelper.BindDDL("城市", ChinaAreaContract.GetSelectList(p => p.ParentCode == provinceCode), 0L, ed_City);
            
            m = ed_City.Items.FindByText(entity.City);
            if (m != null)
                ed_City.SelectedValue = m.Value;

            var cityCode = ed_City.GetValue();
            FineUIListHelper.BindDDL("县区", ChinaAreaContract.GetSelectList(p => p.ParentCode == cityCode), 0L, ed_Area);
            m = ed_Area.Items.FindByText(entity.Area);
            if (m != null)
                ed_Area.SelectedValue = m.Value;

            var areaCode = ed_Area.GetValue();
            FineUIListHelper.BindDDL("街道", ChinaAreaContract.GetSelectList(p => p.ParentCode == areaCode), 0L, ed_Street);
            m = ed_Street.Items.FindByText(entity.Street);
            if (m != null)
                ed_Street.SelectedValue = m.Value;

            ed_Address.SetValue(entity.Address);
            if (!entity.CustomerLevelId.IsNull())
                ed_CustomerLevel.SetValue(entity.CustomerLevelId);
            ed_CardType.SetValue(entity.CardType);
            ControlSet(entity.CardType);

            if (!entity.LimitProductId.IsNull())
                ed_LimitProduct.SetValue(entity.LimitProductId);
            //ed_Balance.SetValue(entity.Balance);
            //ed_TotalAmount.SetValue(entity.TotalAmount);
            //ed_OnLinePayMoney.SetValue(entity.OnLinePayMoney);
            //ed_CostedMoney.SetValue(entity.CostedMoney);
            //ed_CashBackAmount.SetValue(entity.CashBackAmount);
            //ed_Integral.SetValue(entity.Integral);
            //ed_IsServiceCenter.SetValue(entity.IsServiceCenter);
            //ed_SevriceCenterActiveTime.SetValue(entity.SevriceCenterActiveTime);
            //ed_IsOnline.SetValue(entity.IsOnline);
            //ed_IsLocked.SetValue(entity.IsLocked);
            //ed_StateUpdateTime.SetValue(entity.StateUpdateTime);
            //ed_Status.SetValue(entity.Status);
            //ed_ActivateTime.SetValue(entity.ActivateTime);
            ed_Introduction.SetValue(entity.Introduction);
            ed_Remark.SetValue(entity.Remark);
        }

        protected override OperationResult SaveData(Customer entity)
        {
            if (PageMode == pagemode.Add)
            {
                entity.User = new User();
                entity.User.Id = EntityId;
                entity.User.Enable = true;
                entity.User.UserType = 1;
            }

            entity.User.UserName = ed_CardNo.GetValue().ToString();
            entity.User.Name = ed_Name.GetValue().ToString();
            if (ed_Password.GetValue() != CONFIGPASSWORD)
            {
                entity.User.PasswordHash = PasswordHasher.HashPassword(ed_Password.GetValue());
            }

            entity.Name = ed_Name.GetValue().ToString();
            entity.CardNo = ed_CardNo.GetValue().ToString();
            entity.Password = ed_Password.GetValue().ToString();
            entity.Sex = ed_Sex.GetValue().ToInt();
            entity.MobilePhone = ed_MobilePhone.GetValue().ToString();
            entity.IDCard = ed_IDCard.GetValue().ToString();
            //entity.ImageUrl = ed_ImageUrl.GetValue().ToString();
            entity.CardBank = ed_CardBank.GetValue().ToString();
            entity.CardName = ed_CardName.GetValue().ToString();
            if (ed_Province.GetValue() != "0")
                entity.Province = ed_Province.SelectedText;
            else
                entity.Province = "";

            if (ed_City.GetValue() != "0")
                entity.City = ed_City.SelectedText;
            else
                entity.City = "";

            if (ed_Area.GetValue() != "0")
                entity.Area = ed_Area.SelectedText;
            else
                entity.Area = "";

            if (ed_Street.GetValue() != "0")
                entity.Street = ed_Street.SelectedText;
            else
                entity.Street = "";
            entity.Address = ed_Address.GetValue().ToString();
            if (ed_CustomerLevel.GetValue().ToLong() == 0L)
                entity.CustomerLevelId = 0L;
            else
                entity.CustomerLevelId = ed_CustomerLevel.GetValue().ToLong();
            entity.CardType = ed_CardType.GetValue().ToInt();
            if (ed_LimitProduct.GetValue().ToLong() == 0L)
                entity.LimitProductId = null;
            else
                entity.LimitProductId = ed_LimitProduct.GetValue().ToLong();

            if (entity.CardType == 2 && entity.LimitProductId == null)
                return new OperationResult(OperationResultType.Error, "请限定此卡可消费的油种类");

            //entity.Balance = ed_Balance.GetValue().ToDouble();
            //entity.TotalAmount = ed_TotalAmount.GetValue().ToDouble();
            //entity.OnLinePayMoney = ed_OnLinePayMoney.GetValue().ToDouble();
            //entity.CostedMoney = ed_CostedMoney.GetValue().ToDouble();
            //entity.CashBackAmount = ed_CashBackAmount.GetValue().ToDouble();
            //entity.Integral = ed_Integral.GetValue().ToDouble();
            //entity.IsServiceCenter = ed_IsServiceCenter.GetValue().ToBool();
            //entity.SevriceCenterActiveTime = ed_SevriceCenterActiveTime.GetValue().ToNullableDateTime();
            //if(entity.SevriceCenterActiveTime ==DateTime.MinValue)
            //    entity.SevriceCenterActiveTime = null;
            //entity.IsOnline = ed_IsOnline.GetValue().ToBool();
            //entity.IsLocked = ed_IsLocked.GetValue().ToBool();
            //entity.StateUpdateTime = ed_StateUpdateTime.GetValue().ToNullableDateTime();
            //if(entity.StateUpdateTime ==DateTime.MinValue)
            //    entity.StateUpdateTime = null;
            //entity.Status = ed_Status.GetValue().ToBool();
            //entity.ActivateTime = ed_ActivateTime.GetValue().ToNullableDateTime();
            //if (entity.ActivateTime == DateTime.MinValue)
            //    entity.ActivateTime = null;

            if (PageMode == pagemode.Add)
            {
                entity.Balance = 0.0;
                entity.TotalAmount = 0.0;
                entity.OnLinePayMoney = 0.0;
                entity.CostedMoney = 0.0;
                entity.CashBackAmount = 0.0;
                entity.Integral = 0.0;
                entity.IsOnline = false;
                entity.IsLocked = false;
                entity.StateUpdateTime = null;
                entity.Status = true;
                entity.ActivateTime = System.DateTime.Now;
                entity.ActivateId = Operator.UserId;
            }

            entity.Introduction = ed_Introduction.GetValue().ToString();
            entity.Remark = ed_Remark.GetValue().ToString();
            return base.SaveData(entity);
        }


        protected void ed_Province_SelectedIndexChanged(object sender, EventArgs e)
        {
            var provinceCode = ed_Province.GetValue();
            FineUIListHelper.BindDDL("城市", ChinaAreaContract.GetSelectList(p => p.ParentCode == provinceCode), 0L, ed_City);
            var cityCode = ed_City.GetValue();
            FineUIListHelper.BindDDL("县区", ChinaAreaContract.GetSelectList(p => p.ParentCode == cityCode), 0L, ed_Area);
            var areaCode = ed_Area.GetValue();
            FineUIListHelper.BindDDL("街道", ChinaAreaContract.GetSelectList(p => p.ParentCode == areaCode), 0L, ed_Street);
        }

        protected void ed_City_SelectedIndexChanged(object sender, EventArgs e)
        {
            var cityCode = ed_City.GetValue();
            FineUIListHelper.BindDDL("县区", ChinaAreaContract.GetSelectList(p => p.ParentCode == cityCode), 0L, ed_Area);
            var areaCode = ed_Area.GetValue();
            FineUIListHelper.BindDDL("街道", ChinaAreaContract.GetSelectList(p => p.ParentCode == areaCode), 0L, ed_Street);
        }

        protected void ed_Area_SelectedIndexChanged(object sender, EventArgs e)
        {
            var areaCode = ed_Area.GetValue();
            FineUIListHelper.BindDDL("街道", ChinaAreaContract.GetSelectList(p => p.ParentCode == areaCode), 0L, ed_Street);
        }
    }
}
